//
//  NSString+Extension.m
//  Fireball
//
//  Created by 俞渊华 on 2017/12/4.
//  Copyright © 2017年 俞渊华. All rights reserved.
//

#import "NSString+Extension.h"

@implementation NSString (Extension)

- (CGSize)xm_sizeWithFont:(UIFont *)font maxW:(CGFloat)maxW{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, [self length])];
    
    CGSize realSize = CGSizeZero;
    
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
    CGRect textRect = [self boundingRectWithSize:CGSizeMake(maxW, CGFLOAT_MAX)
                                         options:NSStringDrawingUsesLineFragmentOrigin
                                      attributes:@{NSFontAttributeName:font,NSParagraphStyleAttributeName:style}
                                         context:nil];
    realSize = textRect.size;
#else
    realSize = [self sizeWithFont:font constrainedToSize:size];
#endif
    
    realSize.width = ceilf(realSize.width);
    realSize.height = ceilf(realSize.height);
    return realSize;
}

- (CGSize)xm_sizeWithFont:(UIFont *)font{
    return [self xm_sizeWithFont:font maxW:MAXFLOAT];
}
@end
